package com.ruoyi.business.service.impl.quickrepay;

import com.ruoyi.bizsys.domain.CustReduceData;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.domain.ReduceConfig;
import com.ruoyi.bizsys.mapper.CustReduceDataMapper;
import com.ruoyi.business.Vo.CustReduceRes;
import com.ruoyi.business.service.CustReduceProess;
import com.ruoyi.bizsys.utils.BizConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author wqg
 * @description 减免次数判断
 * @date 2022/4/7
 */
@Slf4j
@Order(3)
@Component
public class ReduceNumLimitImpl implements CustReduceProess {
    @Autowired
    CustReduceDataMapper custReduceDataMapper;
    @Override
    public CustReduceRes handle(ReduceConfig reduceConfig, LoanIou loanIou, CustReduceRes custReduceRes) {
        CustReduceData custReduceData = custReduceDataMapper.selectById(loanIou.getCustNo());
        if(null==custReduceData){
            // 创建custReduceDataMapper
            custReduceData = insertCustReduceData(loanIou.getCustNo());
        }
        if(custReduceData.getNumUse()>=reduceConfig.getNumLimit()){
            log.info("CustReduceProess:减免次数用完");
            custReduceRes.setIsHave(false);
            return custReduceRes;
        }
        return custReduceRes;
    }

    /**
     * 初始化个人减免数据
     * @param custNo
     * @return
     */
    private CustReduceData insertCustReduceData(String custNo) {
        CustReduceData custReduceData = new CustReduceData();
        custReduceData.setCustNo(custNo);
        custReduceData.setNumUse(0);
        custReduceData.setState(BizConstants.BIZ_STATUS_1);
        custReduceData.setStatus(BizConstants.BIZ_STATUS_1);
        custReduceDataMapper.insert(custReduceData);
        return custReduceData;
    }

}
